package com.example.petadopt.service;

import com.example.petadopt.dao.AdoptApplyDao;
import com.example.petadopt.dao.UserDao;
import com.example.petadopt.entity.AdoptApply;
import com.example.petadopt.entity.AdoptionApplication;
import com.example.petadopt.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class AdoptApplyService {
    @Autowired
    private AdoptApplyDao adoptApplyDao;
    @Autowired
    private UserDao userDao;

    // 获取所有申请
    public List<AdoptionApplication> getAllApplications() {
        return adoptApplyDao.findAll();
    }

    // 保存申请
    @Transactional
    public void saveApplication(AdoptionApplication application) {
        adoptApplyDao.save(application);
    }

//     审核申请
    public void reviewApplication(int id, int statusId) {
        AdoptionApplication application = adoptApplyDao.findById(id).orElseThrow(() -> new RuntimeException("申请不存在"));
        application.setStatusId(statusId);
        adoptApplyDao.save(application);
    }

    public List<AdoptionApplication> getApplicationsByUserId(int userId) {
        return adoptApplyDao.findByAdoptUserId(userId);
    }

//    public List<AdoptionApplication> getAdoptApplyWithUserByPetId(Integer petId) {
//        List<AdoptionApplication> adoptApplies = adoptApplyDao.findByPetId(petId);
//        for(AdoptionApplication adoptApply : adoptApplies){
//            User user = userDao.findById(adoptApply.getAdoptUserId());
//            adoptApply.setUser(user);
//        }
//        return adoptApplies;
//    }
    public Optional<AdoptionApplication> findById(int id) {
        return adoptApplyDao.findByAdoptId(id);
    }

    public AdoptionApplication save(AdoptionApplication application) {
        return adoptApplyDao.save(application);
    }
}